#include <math.h>

int u(int x, int y, int img_width, int img_height) {

    float c_x =  floor(img_width / 2);
    float c_y =  floor(img_height / 2);

    float factor = 1/sqrt(2);
    float u = c_x + factor * (x - c_x - y + c_y);

    return (int) u;

}

int v(int x, int y, int img_width, int img_height) {

    float c_x = (int) floor(img_width / 2);
    float c_y = (int) floor(img_height / 2);

    float factor = 1/sqrt(2);
    float v = c_y + factor * (x - c_x + y - c_y);

    return (int) v;

}

void rotar_c (
	unsigned char *src,
	unsigned char *dst,
	int m,
	int n,
	int src_row_size,
	int dst_row_size
) {

    int indice_columna = 0;
    int indice_fila = 0;

    while(indice_fila < m) {
        while (indice_columna < n) {
   
            unsigned char dato = 0;
            
            int col_origen = u(indice_columna, indice_fila, n,m);
            int fila_origen = v(indice_columna, indice_fila, n,m);

            if (0 <= col_origen && col_origen < n &&
                0 <= fila_origen && fila_origen < m) {

                dato = src[fila_origen * src_row_size + col_origen];
            }


            dst[indice_fila * dst_row_size + indice_columna] = dato;
            indice_columna++;
        
        }

    indice_fila++;
    indice_columna = 0;

    }


}
